//----------------------------------Include------------------------------------
//-----------------------------------------------------------------------------

//-----------------------------------Type--------------------------------------
typedef struct{
  float   Temp;
  float   Ohms;
}t_TempOhms;
//-----------------------------------------------------------------------------

//--------------------------------Static Var-----------------------------------
static const t_TempOhms  nt_NTC_tab[] = {
#if 0
{-40,  336600,  },
{-39,  315000,  },
{-38,  295000,  },
{-37,  276400,  },
{-36,  259000,  },
{-35,  242800,  },
{-34,  227800,  },
{-33,  213800,  },
{-32,  200600,  },
{-31,  188400,  },
{-30,  177000,  },
{-29,  166400,  },
{-28,  156500,  },
{-27,  147200,  },
{-26,  138500,  },
{-25,  130400,  },
{-24,  122900,  },
{-23,  115800,  },
{-22,  109100,  },
{-21,  102900,  },
{-20,  97120 ,  },
{-19,  91660 ,  },
{-18,  86540 ,  },
{-17,  81720 ,  },
{-16,  77220 ,  },
{-15,  72980 ,  },
{-14,  69000 ,  },
{-13,  65260 ,  },
{-12,  61760 ,  },
{-11,  58460 ,  },
{-10,  55340 ,  },
{-9,   52420 ,  },
{-8,   49660 ,  },
{-7,   47080 ,  },
{-6,   44640 ,  },
{-5,   42340 ,  },
{-4,   40160 ,  },
{-3,   38120 ,  },
{-2,   36200 ,  },
{-1,   34380 ,  },
{0,    32660 ,  },
{1,    31040 ,  },
{2,    29500 ,  },
{3,    28060 ,  },
{4,    26680 ,  },
{5,    25400 ,  },
{6,    24180 ,  },
{7,    23020 ,  },
{8,    21920 ,  },
{9,    20880 ,  },
{10,   19900 ,  },
{11,   18970 ,  },
{12,   18090 ,  },
{13,   17260 ,  },
{14,   16460 ,  },
{15,   15710 ,  },
{16,   15000 ,  },
{17,   14320 ,  },
{18,   13680 ,  },
{19,   13070 ,  },
#endif
{20,   12490 ,  },
//{21,   11940 ,  },
{22,   11420 ,  },
//{23,   10920 ,  },
{24,   10450 ,  },
//{25,   10000 ,  },
{26,   9574  ,  },
//{27,   9166  ,  },
{28,   8778  ,  },
//{29,   8408  ,  },
{30,   8058  ,  },
//{31,   7722  ,  },
{32,   7404  ,  },
//{33,   7098  ,  },
{34,   6808  ,  },
//{35,   6532  ,  },
{36,   6268  ,  },
//{37,   6016  ,  },
{38,   5776  ,  },
//{39,   5546  ,  },
{40,   5326  ,  },
//{41,   5118  ,  },
{42,   4918  ,  },
//{43,   4726  ,  },
{44,   4544  ,  },
//{45,   4368  ,  },
{46,   4202  ,  },
//{47,   4042  ,  },
{48,   3888  ,  },
//{49,   3742  ,  },
{50,   3602  ,  },
//{51,   3468  ,  },
{52,   3340  ,  },
//{53,   3216  ,  },
{54,   3098  ,  },
//{55,   2986  ,  },
{56,   2878  ,  },
//{57,   2774  ,  },
{58,   2674  ,  },
//{59,   2580  ,  },
{60,   2488  ,  },
//{61,   2400  ,  },
{62,   2316  ,  },
//{63,   2234  ,  },
{64,   2158  ,  },
//{65,   2082  ,  },
{66,   2012  ,  },
//{67,   1942  ,  },
{68,   1876  ,  },
//{69,   1813  ,  },
{70,   1751  ,  },
//{71,   1693  ,  },
{72,   1637  ,  },
//{73,   1582  ,  },
{74,   1530  ,  },
//{75,   1480  ,  },
{76,   1432  ,  },
//{77,   1385  ,  },
{78,   1341  ,  },
//{79,   1298  ,  },
{80,   1256  ,  },
//{81,   1216  ,  },
{82,   1178  ,  },
//{83,   1141  ,  },
{84,   1105  ,  },
//{85,   1071  ,  },
{86,   1038  ,  },
//{87,   1006  ,  },
{88,   975   ,  },
//{89,   945.2 ,  },
{90,   916.4 ,  },
//{91,   888.8 ,  },
{92,   862   ,  },
//{93,   836.4 ,  },
{94,   811.4 ,  },
//{95,   787.4 ,  },
{96,   764.2 ,  },
//{97,   741.8 ,  },
{98,   720.2 ,  },
//{99,   699.4 ,  },
{100,  679.2 ,  },
#if 0
{101,  659.6 ,  },
{102,  640.8 ,  },
{103,  622.6 ,  },
{104,  605   ,  },
{105,  588   ,  },
{106,  571.4 ,  },
{107,  555.6 ,  },
{108,  540.2 ,  },
{109,  525.2 ,  },
{110,  510.8 ,  },
{111,  496.8 ,  },
{112,  483.2 ,  },
{113,  470.2 ,  },
{114,  457.4 ,  },
{115,  445.2 ,  },
{116,  433.4 ,  },
{117,  421.8 ,  },
{118,  410.6 ,  },
{119,  399.8 ,  },
{120,  389.4 ,  },
{121,  379.2 ,  },
{122,  369.4 ,  },
{123,  359.8 ,  },
{124,  350.6 ,  },
{125,  341.6 ,  },
{126,  332.8 ,  },
{127,  324.4 ,  },
{128,  316.2 ,  },
{129,  308.2 ,  },
{130,  300.6 ,  },
{131,  293   ,  },
{132,  285.8 ,  },
{133,  278.8 ,  },
{134,  272   ,  },
{135,  265.2 ,  },
{136,  258.8 ,  },
{137,  252.6 ,  },
{138,  246.4 ,  },
{139,  240.6 ,  },
{140,  234.8 ,  },
{141,  229.2 ,  },
{142,  223.8 ,  },
{143,  218.4 ,  },
{144,  213.4 ,  },
{145,  208.4 ,  },
{146,  203.6 ,  },
{147,  198.8 ,  },
{148,  194.2 ,  },
{149,  189.7 ,  },
{150,  185.4 ,  }
#endif
};

static unsigned int nu32_NTC_tab_len = sizeof(nt_NTC_tab)/sizeof(nt_NTC_tab[0]);
//-----------------------------------------------------------------------------

//--------------------------------Static Func----------------------------------
static float _Lookup_Tab(const t_TempOhms* pTab, int tabLen, float Ohms)
{
  int   low    = 0;
  int   high   = tabLen - 1;
  int   mid    = 0;
  float k      = 0;
  float b      = 0;
  float midVal = 0;

  if(Ohms > pTab[0].Ohms){
    return pTab[0].Ohms;
  }else if(Ohms < pTab[tabLen-1].Ohms){
    return pTab[tabLen-1].Ohms;
  }

  while(low <= high){
    mid    = low + ((high - low) / 2);
    midVal = pTab[mid].Ohms;
  
    if(midVal < Ohms){
      high = mid - 1;
    }else if (midVal > Ohms){
      low = mid + 1;
    }else{
      return pTab[mid].Temp;
    }
  }

  if(low > high){
    mid = low;
    low = high;
    high = mid;
  }

  k = (pTab[high].Temp - pTab[low].Temp) / (pTab[high].Ohms - pTab[low].Ohms);
  b =  pTab[low].Temp - k * pTab[low].Ohms;
  midVal = k * Ohms + b;

  return midVal;
}
//--------------------------------Public Func----------------------------------
float MyNTC_Ohms2Temp(float Ohms)
{
  return _Lookup_Tab(nt_NTC_tab,  nu32_NTC_tab_len,  Ohms);
}

//-------------------------------------EOF-------------------------------------




